/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.简化路径;

import java.util.LinkedList;

public class Main {

    public static void main(String arg[]) {//main--来自高金磊

        new Main().simplifyPath("/a/./b/../c/");
    }

    public String simplifyPath(String path) {
        do {
            path = path.replaceAll("//", "/");
        } while (path.contains("//"));
        do {
            path = path.replaceAll("/\\./", "/");
        } while (path.contains("/./"));
//        System.out.println(path);
        LinkedList<String> data = new LinkedList<>();
        for (String s : path.split("/")) {
            if (s.equals("..")) {
                data.pollLast();
                continue;
            }
            if (s.equals("") || s.equals("."))
                continue;
            data.add(s);
        }
        StringBuilder stringBuilder = new StringBuilder("/");
        for (String datum : data) {
            stringBuilder.append(datum).append("/");
        }
        if (stringBuilder.length() > 1)
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }
}
